Completed
Push — phpunit ( 7f2080...75f541 )
by Marcos
14:28 queued 10:45
created

PassmanImporter.clippers.readFile   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 45
rs 8.8571
1
// Importers should always start with this
2
var PassmanImporter = PassmanImporter || {};
1 ignored issue
show
Bug introduced by
The variable PassmanImporter seems to be never initialized.
Loading history...
3
(function(window, $, PassmanImporter) {
4
	'use strict';
5
	PassmanImporter.clippers = {
6
		info: {
7
			name: 'Clipperz.is',
8
			id: 'clippers',
9
			description: 'Go to menu -> Export -> Download HTML + JSON. Fields will be imported as custom fields.'
10
		}
11
	};
12
13
	PassmanImporter.clippers.readFile = function (file_data) {
14
		return new C_Promise(function() {
0 ignored issues
show
Bug introduced by
The variable C_Promise seems to be never declared. If this is a global, consider adding a /** global: C_Promise */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
			var credential_list = [];
16
			var re = /<textarea>(.*?)<\/textarea>/gi;
17
			var matches = re.exec(file_data);
18
			if(matches){
19
				var raw_json = matches[0].substring(10);
20
				raw_json = PassmanImporter.htmlDecode(raw_json.slice(0, -11));
21
				var json_objects = PassmanImporter.readJson(raw_json);
22
				for(var i = 0; i < json_objects.length; i++){
23
					var card = json_objects[i];
24
					re = /(\w+)/gi;
25
					var tags = card.label.match(re);
26
					card.label = card.label.replace(tags.join(' '), '').trim();
27
					tags = tags.map(function(item){ return {text: item.replace('', '') }});
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
28
29
30
					var _credential = PassmanImporter.newCredential();
31
					_credential.label = card.label;
32
					_credential.description = card.data.notes;
33
					_credential.tags = tags;
34
					for(var field in card.currentVersion.fields){
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
35
						var field_data = card.currentVersion.fields[field];
36
						_credential.custom_fields.push(
37
							{
38
								'label': field_data.label,
39
								'value': field_data.value,
40
								'secret': (field_data.hidden == true)
0 ignored issues
show
Coding Style introduced by
It is recommended to use === to compare with true.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
41
							}
42
						)
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
43
					}
44
					if(_credential.label){
45
						credential_list.push(_credential);
46
					}
47
					var progress = {
48
						percent: i/json_objects.length*100,
49
						loaded: i,
50
						total: json_objects.length
51
					};
52
					this.call_progress(progress);
53
				}
54
			}
55
			this.call_then(credential_list);
56
		});
57
	};
58
})(window, $, PassmanImporter);
59